Page({
    /**
     * 页面的初始数据
     */
    data: {
      pageNum: 1,
      dataList: [],
      pageTotal: 0,
      // 1. 定义基础域名（根据实际后端地址修改）
      baseUrl: 'http://localhost:8080' 
    },
  
    /**
     * 获取数据的核心方法
     */
    getData() {
      wx.showLoading({
        title: '数据正在加载',
      });
      wx.request({
        url: 'http://localhost:8080/counselor/page?post=1',
        data: {
          channelId: 409,
          pageNum: this.data.pageNum,
          pageSize: 15,
        },
        method: 'GET',
        success: (resp) => {
          console.log("当前请求页码：", this.data.pageNum);
          console.log("后端返回数据：", resp.data);
  
          const totalPage = resp.data.data.pageTotal || 0;
          let currentPageData = resp.data.data.list || [];
  
          
          currentPageData = currentPageData.map(item => {
            if (item.headImageUrl) {
              if (!item.headImageUrl.startsWith('http')) {
                item.headImageUrl = this.data.baseUrl + item.headImageUrl;
              }
            }
            return item;
          });
  
          // 3. 合并数据并更新
          this.setData({
            dataList: this.data.dataList.concat(currentPageData),
            pageTotal: totalPage
          });
        },
        complete: () => {
          wx.hideLoading();
          wx.stopPullDownRefresh();
        }
      });
    },
  
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      this.getData();
    },
  
    onPullDownRefresh() {
      this.setData({
        pageNum: 1,
        dataList: [],
        pageTotal: 0,
      }, () => {
        this.getData();
      });
    },
  
    onReachBottom() {
      if (this.data.pageNum >= this.data.pageTotal) {
        wx.showToast({
          title: '已经到达最底部啦!',
          icon: 'none'
        });
        return;
      }
      this.setData({
        pageNum: this.data.pageNum + 1
      }, () => {
        this.getData();
      });
    },
  
    onShareAppMessage() {
      return {};
    }
  });